import 'dart:async';
import 'dart:convert';

// 模拟异步查找入口点
Future<String> findEntryPoint() async {
  await Future.delayed(Duration(milliseconds: 200));
  return 'entry_point';
}

// 模拟运行可执行文件
Future<int> runExecutable(String entryPoint, List<String> args) async {
  await Future.delayed(Duration(milliseconds: 200));
  return 0;
}

// 模拟刷新并退出
Future<void> flushThenExit([int? code]) async {
  await Future.delayed(Duration(milliseconds: 100));
  print('flushThenExit: $code');
}

// 模拟 httpClient.read
Future<String> httpRead(String url) async {
  await Future.delayed(Duration(milliseconds: 100));
  return 'http result from $url';
}

// 模拟耗时操作
Future<String> costlyQuery(String url) async {
  await Future.delayed(Duration(milliseconds: 200));
  return 'costly result';
}
Future<void> expensiveWork(String value) async {
  await Future.delayed(Duration(milliseconds: 100));
}
Future<void> lengthyComputation() async {
  await Future.delayed(Duration(milliseconds: 100));
}

// Future/then链式写法
void runUsingFuture(List<String> args) {
  findEntryPoint()
      .then((entryPoint) {
        return runExecutable(entryPoint, args);
      })
      .then(flushThenExit);
}

// async/await写法
Future<void> runUsingAsyncAwait(List<String> args) async {
  var entryPoint = await findEntryPoint();
  var exitCode = await runExecutable(entryPoint, args);
  await flushThenExit(exitCode);
}

// Future链式异常处理
Future<void> futureChainWithError(String url) async {
  Future result = costlyQuery(url);
  result
      .then((value) => expensiveWork(value))
      .then((_) => lengthyComputation())
      .then((_) => print('Done!'))
      .catchError((exception) {
        print('Caught error: $exception');
      });
}

// async/await异常处理
Future<void> asyncAwaitWithError(String url) async {
  try {
    final value = await costlyQuery(url);
    await expensiveWork(value);
    await lengthyComputation();
    print('Done!');
  } catch (e) {
    print('Caught error: $e');
  }
}

// Future.wait 示例
Future<void> waitAll() async {
  Future<void> deleteLotsOfFiles() async {
    await Future.delayed(Duration(milliseconds: 100));
    print('deleteLotsOfFiles done');
  }
  Future<void> copyLotsOfFiles() async {
    await Future.delayed(Duration(milliseconds: 100));
    print('copyLotsOfFiles done');
  }
  Future<void> checksumLotsOfOtherFiles() async {
    await Future.delayed(Duration(milliseconds: 100));
    print('checksumLotsOfOtherFiles done');
  }
  await Future.wait([
    deleteLotsOfFiles(),
    copyLotsOfFiles(),
    checksumLotsOfOtherFiles(),
  ]);
  print('Done with all the long steps!');
}

// Stream 示例
Stream<int> countStream(int max) async* {
  for (int i = 1; i <= max; i++) {
    await Future.delayed(Duration(milliseconds: 100));
    yield i;
  }
}

Future<void> streamDemo() async {
  await for (var value in countStream(3)) {
    print('Stream value: $value');
  }
}

void main() async {
  print('--- Future/then 链式写法 ---');
  runUsingFuture(['arg1', 'arg2']);
  await Future.delayed(Duration(milliseconds: 500));

  print('--- async/await 写法 ---');
  await runUsingAsyncAwait(['arg1', 'arg2']);

  print('--- Future 链式异常处理 ---');
  await futureChainWithError('http://example.com');
  await Future.delayed(Duration(milliseconds: 500));

  print('--- async/await 异常处理 ---');
  await asyncAwaitWithError('http://example.com');

  print('--- Future.wait 并发等待 ---');
  await waitAll();

  print('--- Stream 示例 ---');
  await streamDemo();

  print('全部异步任务结束');
}
// 文件流、按钮事件、文件系统等高级用法因依赖 IO/HTML 环境，已注释。
// 你可以在 Dart/Flutter 项目中参考原始写法实现。